﻿using Domian.Core.Model;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Domian.Core.Ipml
{
    public class PostImageService : IPostImageService
    {
        private readonly string path;

        public PostImageService(IOptions<ImagePath> options)
        {
            path = options.Value.ImageLocalPath;

        }
        public CommonResult saveImage(string base64String)
        {
            CommonResult result = new CommonResult();
            try
            {
                byte[] arr = Convert.FromBase64String(base64String);
                string imageName = path + Guid.NewGuid().ToString() + ".jpg";

                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                using (FileStream fs = new FileStream(imageName, FileMode.Create))
                {
                    fs.Write(arr, 0, arr.Length);
                    fs.Flush();
                    fs.Close();
                }
            }
            catch (Exception ex)
            {
                result.BizCode = 99;
                result.BizAlertMsg = ex.ToString();
            }
            return result;
        }
    }
}
